import { getDepartments } from "@/api/admin/system/department";
import { handleTree } from "@/utils/tools";
import { onMounted, ref } from "vue";

function useDept() {
  const deptTree = ref<any>([{ id: 0, name: "顶级部门", children: null }]);
  const normalTree = ref<any>();
  const getDepts = async () => {
    const res = await getDepartments();
    const lists = res.data.lists.map((item) => ({
      ...item,
      name: item.dept_name,
    }));
    // 获取菜单树
    const children = handleTree(lists, "id", "parent_id", "children").tree;
    deptTree.value[0].children = children;
    normalTree.value = children;
  };

  onMounted(() => {
    getDepts();
  });
  return {
    deptTree,
    normalTree,
  };
}

export default useDept;
